Preserve non-service parameters and harden POST binding source handling in OpenAPI metadata generation#730
Conversation
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes OpenAPI/ApiExplorer metadata generation so that controller action parameters injected from DI ([FromServices] / BindingSource.Services) are excluded from generated API parameter documentation, while normal request parameters (body/query) remain documented.
Changes:
- Updated
MetadataApiDescriptionProviderto skip service-injected parameters when building parameter descriptions for bothGETandPOST. - Added
IsServiceParameterhelper to detect service parameters viaBindingSource.Servicesand explicit[FromServices]attributes. - Added regression tests covering
GETandPOSTactions that include a DI parameter plus a normal request parameter.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Modules/Grand.Module.Api/ApiExplorer/MetadataApiDescriptionProvider.cs | Skips DI/service parameters when generating ApiExplorer/OpenAPI parameter descriptions. |
| src/Tests/Grand.Module.Api.Tests/ApiExplorer/MetadataApiDescriptionProviderTests.cs | Adds regression tests ensuring [FromServices] does not suppress normal body/query parameters. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
@copilot why endpoint "/Account/Register" looks like this in |
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
It was missing because our metadata provider treated all POST action parameters as body parameters; for |
|
@copilot Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] |
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
Fixed in |
|
@copilot problem still exist |
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
Fixed in |
MetadataApiDescriptionProvider emitted ApiParameterDescription objects without a ParameterDescriptor, which OpenApiDocumentService.GetFormRequestBody dereferences when grouping form parameters, crashing /openapi/v2.json with a NullReferenceException. Populate ParameterDescriptor on every emitted description and add regression tests. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>



Actions that include a
[FromServices]parameter were losing request parameter metadata in generated OpenAPI descriptions (e.g., checkout save and account register endpoints). This change ensures service-injected parameters are excluded from API docs while request parameters remain correctly documented, and prevents invalid runtime/non-request parameter metadata from causing OpenAPI generation failures.API description generation
MetadataApiDescriptionProviderto skip service-bound parameters when building parameter descriptions for bothGETandPOST.BindingSource.Services[FromServices]on controller parametersPOSTparameter source mapping:querybodyPOSTparameter source resolution to:BindingInfois missingCancellationTokenparameters from API parameter descriptionsRegression coverage
MetadataApiDescriptionProvider:POSTaction with[FromServices]+ body model keeps only the body model parameter.GETaction with[FromServices]+ query parameter keeps only the query parameter.POSTaction with missingBindingSourceon[FromServices]still excludes the DI parameter.POSTaction with complex model + simple parameter maps to body + query sources correctly.POSTaction with missing binding source onCancellationTokenexcludes the runtime parameter and keeps the request model.